<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="患者ID" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入患者ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:questionnaire:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:questionnaire:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:questionnaire:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handlePatientExport"
        >导出患者用户数据</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出调查问卷数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questionnaireList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="患者ID" align="center" prop="patientId" />
      <el-table-column label="第1题" align="center" prop="question1" />
      <el-table-column label="第2题" align="center" prop="question2" />
      <el-table-column label="第3题" align="center" prop="question3" />
      <el-table-column label="第4题" align="center" prop="question4" />
      <el-table-column label="第5题" align="center" prop="question5" />
      <el-table-column label="第6题" align="center" prop="question6" />
      <el-table-column label="第7题" align="center" prop="question7" />
      <el-table-column label="第8题" align="center" prop="question8" />
      <el-table-column label="第9题" align="center" prop="question9" />
      <el-table-column label="第10题" align="center" prop="question10" />
      <el-table-column label="第11题" align="center" prop="question11" />
      <el-table-column label="第12题" align="center" prop="question12" />
      <el-table-column label="第13题" align="center" prop="question13" />
      <el-table-column label="第14题" align="center" prop="question14" />
      <el-table-column label="第15题" align="center" prop="question15" />
      <el-table-column label="第16题" align="center" prop="question16" />
      <el-table-column label="第17题" align="center" prop="question17" />
      <el-table-column label="第18题" align="center" prop="question18" />
      <el-table-column label="第19题" align="center" prop="question19" />
      <el-table-column label="第20题" align="center" prop="question20" />
      <el-table-column label="第21题" align="center" prop="question21" />
      <el-table-column label="第22题" align="center" prop="question22" />
      <el-table-column label="第23题" align="center" prop="question23" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:questionnaire:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:questionnaire:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改患者调查问卷记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="患者ID" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入患者ID" />
        </el-form-item>
        <el-form-item label="题目1答案" prop="question1">
          <el-input v-model="form.question1" placeholder="请输入题目1答案" />
        </el-form-item>
        <el-form-item label="题目2答案" prop="question2">
          <el-input v-model="form.question2" placeholder="请输入题目2答案" />
        </el-form-item>
        <el-form-item label="题目3答案" prop="question3">
          <el-input v-model="form.question3" placeholder="请输入题目3答案" />
        </el-form-item>
        <el-form-item label="题目4答案" prop="question4">
          <el-input v-model="form.question4" placeholder="请输入题目4答案" />
        </el-form-item>
        <el-form-item label="题目5答案" prop="question5">
          <el-input v-model="form.question5" placeholder="请输入题目5答案" />
        </el-form-item>
        <el-form-item label="题目6答案" prop="question6">
          <el-input v-model="form.question6" placeholder="请输入题目6答案" />
        </el-form-item>
        <el-form-item label="题目7答案" prop="question7">
          <el-input v-model="form.question7" placeholder="请输入题目7答案" />
        </el-form-item>
        <el-form-item label="题目8答案" prop="question8">
          <el-input v-model="form.question8" placeholder="请输入题目8答案" />
        </el-form-item>
        <el-form-item label="题目9答案" prop="question9">
          <el-input v-model="form.question9" placeholder="请输入题目9答案" />
        </el-form-item>
        <el-form-item label="题目10答案" prop="question10">
          <el-input v-model="form.question10" placeholder="请输入题目10答案" />
        </el-form-item>
        <el-form-item label="题目11答案" prop="question11">
          <el-input v-model="form.question11" placeholder="请输入题目11答案" />
        </el-form-item>
        <el-form-item label="题目12答案" prop="question12">
          <el-input v-model="form.question12" placeholder="请输入题目12答案" />
        </el-form-item>
        <el-form-item label="题目13答案" prop="question13">
          <el-input v-model="form.question13" placeholder="请输入题目13答案" />
        </el-form-item>
        <el-form-item label="题目14答案" prop="question14">
          <el-input v-model="form.question14" placeholder="请输入题目14答案" />
        </el-form-item>
        <el-form-item label="题目15答案" prop="question15">
          <el-input v-model="form.question15" placeholder="请输入题目15答案" />
        </el-form-item>
        <el-form-item label="题目16答案" prop="question16">
          <el-input v-model="form.question16" placeholder="请输入题目16答案" />
        </el-form-item>
        <el-form-item label="题目17答案" prop="question17">
          <el-input v-model="form.question17" placeholder="请输入题目17答案" />
        </el-form-item>
        <el-form-item label="题目18答案" prop="question18">
          <el-input v-model="form.question18" placeholder="请输入题目18答案" />
        </el-form-item>
        <el-form-item label="题目19答案" prop="question19">
          <el-input v-model="form.question19" placeholder="请输入题目19答案" />
        </el-form-item>
        <el-form-item label="题目20答案" prop="question20">
          <el-input v-model="form.question20" placeholder="请输入题目20答案" />
        </el-form-item>
        <el-form-item label="题目21答案" prop="question21">
          <el-input v-model="form.question21" placeholder="请输入题目21答案" />
        </el-form-item>
        <el-form-item label="题目22答案" prop="question22">
          <el-input v-model="form.question22" placeholder="请输入题目22答案" />
        </el-form-item>
        <el-form-item label="题目23答案" prop="question23">
          <el-input v-model="form.question23" placeholder="请输入题目23答案" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuestionnaire, getQuestionnaire, delQuestionnaire, addQuestionnaire, updateQuestionnaire } from "@/api/business/questionnaire";

export default {
  name: "Questionnaire",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者调查问卷记录表格数据
      questionnaireList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientId: null,
        question1: null,
        question2: null,
        question3: null,
        question4: null,
        question5: null,
        question6: null,
        question7: null,
        question8: null,
        question9: null,
        question10: null,
        question11: null,
        question12: null,
        question13: null,
        question14: null,
        question15: null,
        question16: null,
        question17: null,
        question18: null,
        question19: null,
        question20: null,
        question21: null,
        question22: null,
        question23: null,
        question24: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patientId: [
          { required: true, message: "患者ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询患者调查问卷记录列表 */
    getList() {
      this.loading = true;
      listQuestionnaire(this.queryParams).then(response => {
        this.questionnaireList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        patientId: null,
        question1: null,
        question2: null,
        question3: null,
        question4: null,
        question5: null,
        question6: null,
        question7: null,
        question8: null,
        question9: null,
        question10: null,
        question11: null,
        question12: null,
        question13: null,
        question14: null,
        question15: null,
        question16: null,
        question17: null,
        question18: null,
        question19: null,
        question20: null,
        question21: null,
        question22: null,
        question23: null,
        question24: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加患者调查问卷记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQuestionnaire(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改患者调查问卷记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQuestionnaire(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuestionnaire(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除患者调查问卷记录编号为"' + ids + '"的数据项？').then(function() {
        return delQuestionnaire(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handlePatientExport() {
      this.download('/system/user/exportPatient', {
        ...this.queryParams
      }, `患者用户数据.xlsx`)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/questionnaire/export', {
        ...this.queryParams
      }, `调查问卷数据.xlsx`)
    }
  }
};
</script>
